package dao

import (
	"database/sql"
	"errors"
	xerrors "github.com/pkg/errors"
	"strconv"

	"main/config"
)

// 自定义notfound，屏蔽数据库差异及供某些业务不时之需
var NotFound error = errors.New("找不到呢，请更换查询条件试试吧~")

var firstTestErrSQL = "SELECT `id`, `name` FROM `test_err` WHERE `id` = ?"

type TestErr struct {
	ID   int
	Name string
}

func FirstByID(ID int) (*TestErr, error) {
	var te TestErr
	var err error
	err = config.DbClient.QueryRow(firstTestErrSQL, ID).Scan(&te.ID, &te.Name)
	if err != nil {
		// 若不处理，则返回te的值默认为零值空值等，导致较难判断是否查到有该行
		if err == sql.ErrNoRows {
			return nil, NotFound
		} else {
			msg := "dao/test_err.go/FirstByID()/config.DbClient.QueryRow.().Scan() | params: ID=" + strconv.Itoa(ID)
			return nil, xerrors.Wrap(err, msg)
		}
	}
	return &te, nil
}
